﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TheMovieDB;

namespace WDTVMetadata
{
    [XmlRoot("details")]
    public class WDTVLiveHubMetadata
    {
        public class Actor
        {
            [XmlElement("name")]
            public string Name { get; set; }
            [XmlElement("role")]
            public string Role { get; set; }

            public Actor ()
            {
            }

            public Actor (string name, string role)
            {
                Name = name;
                Role = role;
            }
        }

        public class UrlClass
        {
            [XmlAttribute("cache")]
            public string Cache { get; set; }
            [XmlAttribute("function")]
            public string Function { get; set; }
            [XmlText]
            public string Text { get; set; }

            public UrlClass()
            {
                Cache = "tmdb-.xml";
                Function = "GetTMDBThumbsById";
                Text = " ";
            }
        }

        [XmlElement("title")]
        public string Name { get; set; }

        [XmlElement("mpaa")]
        public string Certification { get; set; }

        [XmlIgnore]
        public DateTime? Released { set; get; }
        [XmlElement("year")]
        public string ReleasedString
        {
            get { return Released.HasValue ? (Released.Value.ToString("yyyy-MM-dd")) : ""; }
            set
            {
                DateTime d;
                if (DateTime.TryParse(value, out d))
                    Released = d;
                else
                    Released = null;
            }
        }

        [XmlIgnore]
        public TimeSpan Runtime { get; set; }
        [XmlElement("runtime")]
        public string MovieRuntime
        {
            get { return Runtime.TotalMinutes.ToString(); }
            set
            {
                int minutes;
                Runtime = TimeSpan.FromMinutes(int.TryParse(value, out minutes) ? minutes : 0);
            }
        }

        [XmlElement("rating")]
        public decimal Rating { get; set; }

        [XmlElement("trailer")]
        public string Trailer { get; set; }

        [XmlElement("genre", typeof(string))]
        public List<string> Genres { get; set; }

        [XmlElement("studio", typeof(string))]
        public List<string> Studios { get; set; }

        [XmlElement("plot")]
        public string Plot { get; set; }

        [XmlElement("overview")]
        public string Overview { get; set; }

        [XmlElement("director")]
        public string Director { get; set; }

        [XmlElement("actor", typeof(Actor))]
        public List<Actor> Actors { get; set; }

        [XmlElement("url")]
        public UrlClass Url { get; set; }

        [XmlElement("thumbnail", typeof(string))]
        public List<string> Thumbnails { get; set; }

        [XmlElement("backdrop", typeof(string))]
        public List<string> Backdrops { get; set; }

        [XmlArrayAttribute("images")]
        public TmdbImage[] Images { get; set; }

        public WDTVLiveHubMetadata()
        {
        }

        public WDTVLiveHubMetadata(TmdbMovie tmdbMovie)
        {
            Name = tmdbMovie.Name;
            Certification = tmdbMovie.Certification;
            Released = tmdbMovie.Released;
            Runtime = tmdbMovie.Runtime;
            Rating = tmdbMovie.Rating;
            Trailer = tmdbMovie.Trailer;

            List<string> genreList = new List<string>();
            foreach (TmdbCategory category in tmdbMovie.Categories)
            {
                if (category.Type == "genre")
                    genreList.Add(category.Name);
            }
            Genres = genreList;

            List<string> studiosList = new List<string>();
            foreach (TmdbStudio studio in tmdbMovie.Studios)
            {
                studiosList.Add(studio.Name);
            }
            Studios = studiosList;

            Plot = tmdbMovie.Overview;
            Overview = tmdbMovie.Overview;

            foreach (TmdbCastPerson castPerson in tmdbMovie.Cast)
            {
                if (castPerson.Job == "Director")
                    Director = castPerson.Name;
            }

            List<Actor> actorList = new List<Actor>();
            foreach (TmdbCastPerson castPerson in tmdbMovie.Cast)
            {
                if (castPerson.Job == "Actor")
                {
                    Actor actor = new Actor(castPerson.Name, castPerson.Character);
                    actorList.Add(actor);
                }
            }
            Actors = actorList;

            Url = new UrlClass();
  
            List<string> thumbnailList = new List<string>();
            foreach (TmdbImage image in tmdbMovie.Images)
            {
                if (image.Type == TmdbImageType.poster
                    && image.Size == TmdbImageSize.cover)
                {
                    thumbnailList.Add(image.Url);
                }
            }
            Thumbnails = thumbnailList;

            List<string> backdropList = new List<string>();
            foreach (TmdbImage image in tmdbMovie.Images)
            {
                if (image.Type == TmdbImageType.backdrop
                    && image.Size == TmdbImageSize.poster)
                {
                    backdropList.Add(image.Url);
                }
            }
            Backdrops = backdropList;
        }
   }
}
